package http;


import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

public class ParserConfig extends DefaultHandler {
	private boolean xmlhelperDocumentOpened = false;
	private XmlHelperElement xmlhelperTree = new XmlHelperElement("","root","root",null,null,"");
	private XmlHelperElement xmlhelperCurrentElement = xmlhelperTree;
	// Singleton-Pattern:
	private static ParserConfig instance;
	private ParserConfig() {
		xmlhelperCurrentElement = xmlhelperTree;
	}
	public static ParserConfig getInstance() {
		if (instance == null)
			instance = new ParserConfig();
		return instance;
	}
	
    public void startDocument ()
    {
		xmlhelperDocumentOpened = true;
    }
    public void endDocument ()
    {
    	xmlhelperDocumentOpened = false;
    }
    public void startElement (String uri, String name,
		      String qName, Attributes atts)
    {
    	XmlHelperElement e = new XmlHelperElement(uri,name,qName,atts,xmlhelperCurrentElement,"");
    	if (xmlhelperDocumentOpened)
    		xmlhelperCurrentElement.children.add(e);
    	xmlhelperCurrentElement = e;
    }
    public void endElement (String uri, String name, String qName)
    {
    	if (xmlhelperDocumentOpened)
    		xmlhelperCurrentElement = xmlhelperCurrentElement.parent;
    }
    public void characters (char[] ch, int start, int length)
    {
    	if (xmlhelperDocumentOpened)
    		for (int i = start; i < start + length; i++)
    			xmlhelperCurrentElement.content += ch[i];
    }
	public void load(File config) throws SAXException, IOException {
		XMLReader xr = XMLReaderFactory.createXMLReader();
		xr.setContentHandler(this);
		xr.setErrorHandler(this);
	    FileReader r = new FileReader(config);
	    xr.parse(new InputSource(r));
	}

	public List<XmlHelperElement> getValidatingInfo(String page) {
		xmlhelperCurrentElement = xmlhelperTree;
		List<XmlHelperElement> validatingInfo = new ArrayList<XmlHelperElement>();
		for (XmlHelperElement e:xmlhelperCurrentElement.children) {
			if (e.name.toLowerCase().compareTo("sjcconfig") == 0) {
				for (XmlHelperElement e2:e.children) {
					if (e2.name.toLowerCase().compareTo("validator") == 0) {
						for (XmlHelperElement e3:e2.children) {
							if (e3.name.toLowerCase().compareTo(page.toLowerCase()) == 0) {
								 validatingInfo = e3.children;
							}
						}
					}
				}
			}
		}
		return validatingInfo;
	}
	public List<XmlHelperElement> getParsingInfo(String page) {
		xmlhelperCurrentElement = xmlhelperTree;
		List<XmlHelperElement> parsingInfo = new ArrayList<XmlHelperElement>();
		for (XmlHelperElement e:xmlhelperCurrentElement.children) {
			if (e.name.toLowerCase().compareTo("sjcconfig") == 0) {
				for (XmlHelperElement e2:e.children) {
					if (e2.name.toLowerCase().compareTo("parser") == 0) {
						for (XmlHelperElement e3:e2.children) {
							if (e3.name.toLowerCase().compareTo(page.toLowerCase()) == 0) {
								 parsingInfo = e3.children;
							}
						}
					}
				}
			}
		}
		return parsingInfo;
	}
	public List<XmlHelperElement> getRepairInfo(String page) {
		xmlhelperCurrentElement = xmlhelperTree;
		List<XmlHelperElement> repairInfo = new ArrayList<XmlHelperElement>();
		for (XmlHelperElement e:xmlhelperCurrentElement.children) {
			if (e.name.toLowerCase().compareTo("sjcconfig") == 0) {
				for (XmlHelperElement e2:e.children) {
					if (e2.name.toLowerCase().compareTo("repair") == 0) {
						for (XmlHelperElement e3:e2.children) {
							if (e3.name.toLowerCase().compareTo(page.toLowerCase()) == 0) {
								 repairInfo = e3.children;
							}
						}
					}
				}
			}
		}
		return repairInfo;
	}
	public String getConfig(String page) {
		xmlhelperCurrentElement = xmlhelperTree;
		String parsingInfo = null;
		for (XmlHelperElement e:xmlhelperCurrentElement.children) {
			if (e.name.toLowerCase().compareTo("sjcconfig") == 0) {
				for (XmlHelperElement e2:e.children) {
					if (e2.name.toLowerCase().compareTo("config") == 0) {
						for (XmlHelperElement e3:e2.children) {
							if (e3.name.toLowerCase().compareTo(page.toLowerCase()) == 0) {
								 parsingInfo = e3.content;
							}
						}
					}
				}
			}
		}
		return parsingInfo;
	}
}